package com.person.servlet;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.person.bean.ArticleBean;
import com.person.daoImpl.AdminDaoImpl;
import com.person.pojo.ArticleInfo;
import com.person.vo.Article;

/**
 * Servlet implementation class articleAddServlet
 */
@WebServlet("/articleAddServlet")
public class ArticleAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ArticleAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		
		//输出流
		PrintWriter out = response.getWriter();
		
		request.setCharacterEncoding("UTF-8");
		InputStream ips = null;
		
		/*获取数据控制层对象*/
		AdminDaoImpl adi = new AdminDaoImpl();
		
		/*获取session对象*/
		HttpSession session = request.getSession();
		
		/*将用户上传的图片转换为Byte数组，再将其转换为InputStream类型*/
//		ips = new ByteArrayInputStream(request.getParameter("fileUpload").getBytes());
		String title = request.getParameter("title");
		String content = request.getParameter("content");
		String descript = request.getParameter("discript");

		/*获取当前的时间*/
		
		java.util.Date date = new java.util.Date();
		
		
		
		Article atc = new Article();
		
		/*获取当前管理员的id*/
		int userId = (int)request.getSession().getAttribute("userId");
		
		/*封装数据*/
//		atc.setBackimage(ips);
		atc.setTitle(title);
		atc.setContent(content);
		atc.setDecrip(descript);
		atc.setFk_id(userId);
		atc.setDate(date);
		
		ArticleBean atcb = new ArticleBean();
		ArticleInfo atInfo = atcb.getArticleInfo(atc);	//是否成功及文章信息
		
		if(atInfo.isAddSucce()) {
			/*文章添加成功*/
			System.out.print("文章添加成功！");
			session.setAttribute("atInfo", atInfo);
			
			/*创建新网页*/
			//获取文章的数量
			int articleNum = adi.getArticleNum();
			articleNum += 1;
			String articleFileName = "article" + articleNum;
			request.setAttribute("isSusse", 1);
			request.getRequestDispatcher("/backManaSyst/articleAdd.jsp").forward(request, response);
		}else {
			System.out.print("文章添加失败！");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
